const fs = require("fs")
const esprima = require("esprima")
const estraverse = require("estraverse")
const escodegen = require("escodegen")
const iconv = require("iconv-lite")
const base_ast = require("../../tools/es_ast_tool")

function get_enc_strs(ast){
    var code = "";
    estraverse.traverse(ast, {
        enter(node, parent){
            if(node.type==="VariableDeclarator"&&node.id&&node.id.type==="Identifier"){
                if(node.id.name==="_"){
                    code = "var "+escodegen.generate(node)+";";
                }
            }
        }
    })
    return code;
}

function exchange(ast, vars){
    estraverse.replace(ast,{
        enter(node, parent){
            if(node.type==="MemberExpression"&&node.object&&node.object.name==="_"){
                eval(vars);
                var code = escodegen.generate(node);

                var val = eval(code);
                return {
                    type:esprima.Syntax.Literal,
                    value:val,
                    raw:val.toString()
                }
            }
        }
    })
}

function dec_main(ast){
    var var_init = get_enc_strs(ast);
    console.log(var_init);
    // eval(var_init);
    // console.log(_);
    exchange(ast, var_init);
}

var ast = base_ast.get_ast_by_file("./5e2900b8.js");
dec_main(ast);
base_ast.save_ast_to_file(ast, "1.js");
